package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @GetMapping("/page")
    public R<Page<DishDto>> findByPage(Integer page, Integer pageSize, String name) {
        Page<DishDto> dishDtoPage = dishService.findByPage(page, pageSize, name);
        return R.success(dishDtoPage);
    }

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto, HttpSession session) {
        Employee userLogin = (Employee) session.getAttribute("userLogin");
        dishDto.setCreateUser(userLogin.getId());
        dishDto.setUpdateUser(userLogin.getId());
        dishService.save(dishDto);
        return R.success("添加成功");
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        dishService.delete(ids);
        return R.success("删除成功");
    }

    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable long id) {
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto, HttpSession session) {
        Employee userLogin = (Employee) session.getAttribute("userLogin");
        dishDto.setUpdateUser(userLogin.getId());
        dishService.update(dishDto);
        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R<List<DishDto>> findAllByCid(long categoryId, @RequestParam(defaultValue = "1") Integer status) {
        List<DishDto> dishList = dishService.findAllByCid(categoryId, status);
        return R.success(dishList);
    }

    //起售停售
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@RequestParam List<Long> ids, @PathVariable Integer status) {
        dishService.updateStatus(ids, status);
        return R.success("在售状态修改成功");
    }
}
